<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="文件名" prop="fileName">
        <el-input
            v-model="queryParams.fileName"
            placeholder="请输入文件名"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建方式" prop="jarSource">
        <el-select v-model="queryParams.jarSource" placeholder="请选择创建方式">
          <el-option label="git" value="git"></el-option>
          <el-option label="upload" value="upload"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="类型" prop="jarType">
        <el-select v-model="queryParams.jarType" placeholder="请选择类型">
          <el-option label="flink" value="flink"></el-option>
          <el-option label="udf" value="udf"></el-option>
          <el-option label="其他依赖" value="dep"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['resource:jarmanagement:add']"
        >新增
        </el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList(false)"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="jarmanagementList" @selection-change="handleSelectionChange" border  highlight-current-row align="center">
<!--      <el-table-column type="selection" width="55" align="center"/>-->
      <el-table-column label="id" align="center" prop="id"/>
      <el-table-column label="所属项目" align="center" prop="applicationName"/>
      <el-table-column label="文件名" align="center" prop="fileName"/>
      <el-table-column label="jar包类型" align="center" prop="jarType"/>
      <el-table-column label="创建方式" align="center" prop="jarSource"/>
      <el-table-column label="存放路径" align="center" prop="filePath"/>
      <el-table-column label="函数名" align="center" prop="funcName"/>
      <el-table-column label="入口类" align="center" prop="mainClass"/>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column prop="inUse" label="是否使用中" align="center" min-width="200">
        <template slot-scope="scope">
          <TableState v-if="scope.row.inUse" showTitle="使用中" stateType="positive"/>
          <TableState v-else showTitle="未使用" stateType=""/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-if="!scope.row.inUse"
              v-hasPermi="['resource:jarmanagement:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList(false)"
    />

    <el-drawer
        :wrapperClosable='false'
        title="新增jar"
        :visible.sync="open"
        size="70%"
        :before-close="handelClose">
      <addJar ref="addJarRef" @saveOk="saveOk" :form-data="formData"
              :git-project-list="gitProjectList"
              :module-list="moduleList"
              :module-jar-list="moduleJarList"
             />
    </el-drawer>


  </div>
</template>

<script>
import {
  listJarmanagement,
  getJarmanagement,
  delJarmanagement,
  addJarmanagement,
  updateJarmanagement
} from "@/api/resource/jarmanagement";


import TableState from "@/components/TableState";
import addJar from "@/views/resource/jarmanagement/addJar";
import {getAppList} from "@/api/resource/application";

import {getGitProjectList, getModulList, getModulJarList} from "@/api/resource/project";


export default {
  name: "Jarmanagement",
  components: {
    TableState,
    addJar
  },
  data() {
    return {
      gitProjectList: [],
      moduleList: [],
      moduleJarList: [],
      formData: {
        applicationList: [],
        applicationId: '',
        jarSource: '',
        gitProjectList: [],
        gitModuleList: [],
        gitJarList: [],
        gitPro: '',
        gitModule: '',
        gitJarId: '',
        jarType:'',
        mainClass:'',
        funcName:'',
        remark:'',
        fileList:[],
        fileName:'',
        tmpPath:''
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // Jar包管理表格数据
      jarmanagementList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fileName: null,
        jarType: null,
        jarSource: null
      }
    };
  },
  created() {
    this.getList(false);
  },

  mounted() {
    const timer = window.setInterval(() => this.getList(true), 2000)
    this.$once('hook:beforeDestroy', () => {
      clearInterval(timer)
    })
  },

  methods: {
    getGitList() {
      getGitProjectList().then(res => {
        this.gitProjectList = res.rows;
      })
      getModulList().then(res => {
        this.moduleList = res.rows;
      })

      getModulJarList().then(res => {
        this.moduleJarList = res.rows;
      })
    },
    /** 查询Jar包管理列表 */
    getList(isTimer) {
      if (!isTimer) {
        this.loading = true;
      }
      this.getGitList();
      listJarmanagement(this.queryParams).then(response => {
        this.jarmanagementList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList(false);
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },

    clearForm()
    {
      this.getGitList();
      this.formData.jarSource = '';
      this.formData.gitPro = '';
      this.formData.gitModule = '';
      this.formData.gitJarId = '';
      this.formData.jarType='';
      this.formData.mainClass='';
      this.formData.funcName='';
      this.formData.remark='';
      this.formData.fileList=[];
      this.formData.fileName='';
      this.formData.tmpPath='';
    },

    /** 新增按钮操作 */
    handleAdd() {
      getAppList().then(res => {
        if (res.rows.length > 0) {
          this.open = true;
          this.title = "添加Jar包管理";
          this.clearForm();
          this.formData.applicationList = res.rows;
        } else {
          this.$message.warning('当前项目为空，请先创建所属项目！')
        }
      })

    },

    saveOk() {
      this.open = false
      this.clearForm()
      this.getList(false)
    },

    handelClose() {
      this.open = false
      this.clearForm();
      if (this.$refs.addJarRef !== undefined) {
        this.$refs.addJarRef.removeHdfsTmp();
        this.$refs.addJarRef.resetForm();
      }
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除Jar包管理编号为"' + ids + '"的数据项？').then(function () {
        return delJarmanagement(ids);
      }).then(() => {
        this.getList(false);
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('resource/jarmanagement/export', {
        ...this.queryParams
      }, `jarmanagement_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
